package com.xinruke.hostels.systemmanage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.DictInfoCacheExtractor;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwFangjianleixing;
import com.xinruke.hostels.common.po.YwJiaobanmoshi;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.support.interfaces.api.SupportApi;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.YwJiaobanmoshiMapper;
import com.xinruke.hostels.systemmanage.service.WorkModeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class WorkModeServiceImpl implements WorkModeService {

    @Autowired
    private YwJiaobanmoshiMapper ywJiaobanmoshiMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Autowired
    private DictInfoCacheExtractor dictInfoCacheExtractor;

    @Override
    public ResultVO add(WorkModeAddDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanAdd(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        LoginManageResultVO user = manageUserCache.getLoginManageUserInfo();

        YwJiaobanmoshi add = new YwJiaobanmoshi();
        BeanUtils.copyProperties(params, add);
        add.setJiudianId(user.getJiudianId());
        add.setChuangjianshijian(new Date());
        add.setChuangjianrenId(user.getId());
        add.setGengxinrenId(user.getId());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        int insert = ywJiaobanmoshiMapper.insertSelective(add);
        if(insert == 0){
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO modify(WorkModeModifyDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanModify(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        YwJiaobanmoshi modify = new YwJiaobanmoshi();
        BeanUtils.copyProperties(params, modify);
        modify.setGengxinrenId(manageUserCache.getCurrentUserId());
        modify.setGengxinshijian(new Date());
        int count = ywJiaobanmoshiMapper.updateByPrimaryKeySelective(modify);
        if(count == 0){
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO delete(WorkModeDeleteDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanDelete(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        YwJiaobanmoshi delete = new YwJiaobanmoshi();
        BeanUtils.copyProperties(params, delete);
        delete.setGengxinshijian(new Date());
        delete.setGengxinrenId(manageUserCache.getCurrentUserId());
        delete.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        int insert = ywJiaobanmoshiMapper.updateByPrimaryKeySelective(delete);
        if(insert == 0){
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO<RowsDataVO<WorkModeQueryResultDTO>> list(WorkModeQueryListDTO params) {

        RowsDataVO<WorkModeQueryResultDTO> dataVO = new RowsDataVO();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        if (null != params.getMoshi()) {
            conditions.addCondition("moshi", EConditionOperator.EQULS, params.getMoshi());
        }

        MyExample example = new MyExample(YwJiaobanmoshi.class, conditions, params.getOrderFields());
        Page<YwJiaobanmoshi> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwJiaobanmoshi> list = ywJiaobanmoshiMapper.selectByExample(example);

        List<WorkModeQueryResultDTO> dtoList = list.stream().map(n -> {
            WorkModeQueryResultDTO dto = new WorkModeQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setMoshiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.MOSHI, dto.getMoshi()));
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    @Override
    public ResultVO<WorkModeQueryResultDTO> detail(WorkModeDetailDTO params) {

        WorkModeQueryResultDTO dto  = new WorkModeQueryResultDTO();
        YwJiaobanmoshi detail = ywJiaobanmoshiMapper.selectByPrimaryKey(params.getId());

        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        dto.setMoshiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.MOSHI, dto.getMoshi() ));
        return ResultVO.success("获取详情成功", dto);
    }

    private void checkIsCanModify(WorkModeModifyDTO params, ResultVO resultVO) {
    }

    private void checkIsCanAdd(WorkModeAddDTO params, ResultVO resultVO) {
    }

    private void checkIsCanDelete(WorkModeDeleteDTO params, ResultVO resultVO) {
    }
}
